<?php

if (!defined('IN_XLP')) {
    exit('Access Denied');
}

/**
 * Description of userAction
 * 个人中心
 * @author xlp
 */
class userAction extends commonAction {

    function __construct() {
        parent::__construct();
        parent::_checkLogin();
        //获取游戏数据
        parent::_initHeader();
    }

    /*
     * 会员中心
     */

    function index() {
        $this->display();
    }


    /*
     * 个人中心
     */
    function center() {
        $this->display();
    }

    /*
     * 账号安全
     */
    function safe() {
        $this->display();
    }



    /*
     * 修改手机
     */
    function safePhone() {
        $this->display();
    }



    function saveUser() {
        $objData['username'] = $this->_post('username');
        $objData['sex'] = $this->_post('sex' , 1);
        $objData['birthtime'] = $this->_post('birthday');
        $headimgurl = '';
        if (isHave($_FILES['headimgurl']) && !isHave($_FILES['headimgurl']['error'])) { 
            load('upload');
            $myUpload = new Myupload();
            $upload = $myUpload->upload('user/u' . user::$userInfo['uid']);
            if(!$upload[0]){
                $this->JsonReturn($myUpload->getErrorMsg());
            }
            $objData['headimgurl'] = $upload[0]['savepath'];
            $headimgurl = getImgUrl($upload[0]['savepath']);
        }
        M('user')->update($objData , array('uid' => user::$userInfo['uid']));
        $this->JsonReturn('ok' , $headimgurl , 1);
    }


    /*
     * 修改密码
     */
    function changePassword() {
        $old_psw = $this->_post('old_psw');
        $psw_one = $this->_post('psw_one');
        $psw_two = $this->_post('psw_two');
        if(!isHave($old_psw)) {
            $this->JsonReturn('请输入原密码');
        }
        if(!isHave($psw_one)) {
            $this->JsonReturn('请输入新密码');
        }
        if(!isHave($psw_two)) {
            $this->JsonReturn('请输入确认密码');
        }

        if(md5($old_psw) != user::$userInfo['password']) {
            $this->JsonReturn('原密码输入错误');
        }

        if($psw_one != $psw_two) {
            $this->JsonReturn('两次新密码输入不一致');
        }
        M('user')->update(array('password'=>md5($psw_one)) , array('uid'=>user::$userInfo['uid']));
        $this->JsonReturn('修改密码成功!', null , 1);
    }



    /*
     * 发送手机短信
     */
    function sendPasswordOne() {
        //检测短信发送数量
        // $stime = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
        // $etime = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
        // $map['uid'] = user::$userInfo['uid'];
        // $map['createtime[>=]'] = $stime;
        // $map['createtime[<=]'] = $etime;
        // $data = M('sms_log')->where($map)->count();
        // if($data >= 10) {
        //     $this->JsonReturn('您今日短信已超上限');
        // }
        $phone = user::$userInfo['phone'];
        //生成验证码
        $captcha = getRandInt(4);
        S("captcha_old_" . $phone, $captcha, 120); //2分钟内有效

        //加载阿里大鱼短信接口
        V('Sms/sms' , false);
        $data['phone']   = $phone;
        $data['captcha'] = $captcha;
        $result = sms::sendSms($data , 'SMS_80215352');
        if ($result) {
            $save['phone']      = $phone;
            $save['createtime'] = TIME;
            M('sms_log')->insert($save);
            $this->JsonReturn('ok', null, 1);
        } else {
            $error = sms::gerError();
            saveLog('login/error', array('error' => $error));
            $this->JsonReturn('服务忙，请稍后再试~!');
        }

    }


    function sendPasswordTwo() {
        //检测短信发送数量
        // $stime = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
        // $etime = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
        // $map['uid'] = user::$userInfo['uid'];
        // $map['createtime[>=]'] = $stime;
        // $map['createtime[<=]'] = $etime;
        // $data = M('sms_log')->where($map)->count();
        // if($data >= 10) {
        //     $this->JsonReturn('您今日短信已超上限');
        // }
        $phone = $this->_post('phone');

        if(!isHave($phone)) {
            $this->JsonReturn("参数丢失");
        }
        //生成验证码
        $captcha = getRandInt(4);
        S("captcha_new_" . $phone, $captcha, 120); //2分钟内有效

        //加载阿里大鱼短信接口
        V('Sms/sms' , false);
        $data['phone']   = $phone;
        $data['captcha'] = $captcha;
        $result = sms::sendSms($data , 'SMS_80215352');
        if ($result) {
            $save['phone']      = $phone;
            $save['createtime'] = TIME;
            M('sms_log')->insert($save);
            $this->JsonReturn('ok', null, 1);
        } else {
            $error = sms::gerError();
            saveLog('login/error', array('error' => $error));
            $this->JsonReturn('服务忙，请稍后再试~!');
        }

    }


    function checkCaptchaOne() {
        $captcha = $this->_post('captcha');
        $phone = user::$userInfo['phone'];
        //校对验证码
        if(!S("captcha_old_" . $phone)) {
            $this->JsonReturn('验证码已失效，请重新发送');
        }
        if($captcha != S("captcha_old_" . $phone)) {
            $this->JsonReturn('您输入的验证码有误');
        }
        $this->JsonReturn('ok' , null , 1);
    }




    function checkCaptchaTwo() {
        $captcha = $this->_post('captcha');
        $phone = $this->_post('new_phone');
        //校对验证码
        if(!S("captcha_new_" . $phone)) {
            $this->JsonReturn('验证码已失效，请重新发送');
        }
        if($captcha != S("captcha_new_" . $phone)) {
            $this->JsonReturn('您输入的验证码有误');
        }

        $objData['phone'] = $phone;
        M('user')->update($objData , array('uid' => user::$userInfo['uid']));
        $this->JsonReturn('ok' , $phone , 1);
    }


    //检测手机是否被注册
    function checkPhoneRegister() {
        $map['phone'] = $this->_post('phone');
        $data = M('user')->field('uid')->where($map)->find();
        if(!$data){
            $this->JsonReturn('ok' , null , 1);
        }else{
            $this->JsonReturn('该手机号已注册，无法绑定');
        }
    }

}



